In [1]:
# This tutorial discusses how to define functions within Python code.
# Once defined, the functions can be evaluated, differentiated, integrated,
# plotted, ...  
import numpy as np
import matplotlib.pyplot as plt
In [2]:
# The notation used to define a function is the following:
def f(x):
    return np.sin(x)
In [3]:
# You can all your function anything, it doesn't have to be f.  Also, the 
# variables are defined within the function and do not need to be declared
# outside of the function.
    
# Once defined, it's easy to evaluate the function at any value of x.
print(f(0))
print(f(np.pi/4))
print(f(np.pi/2))
0.0
0.7071067811865476
1.0
In [4]:
# We can also input a vector of x-values and evaluate the function at each
# value of x contained in the array.
xx = np.arange(0,2*np.pi,0.1)
y = f(xx)
y
Out[4]:
array([ 0.        ,  0.09983342,  0.19866933,  0.29552021,  0.38941834,
        0.47942554,  0.56464247,  0.64421769,  0.71735609,  0.78332691,
        0.84147098,  0.89120736,  0.93203909,  0.96355819,  0.98544973,
        0.99749499,  0.9995736 ,  0.99166481,  0.97384763,  0.94630009,
        0.90929743,  0.86320937,  0.8084964 ,  0.74570521,  0.67546318,
        0.59847214,  0.51550137,  0.42737988,  0.33498815,  0.23924933,
        0.14112001,  0.04158066, -0.05837414, -0.15774569, -0.2555411 ,
       -0.35078323, -0.44252044, -0.52983614, -0.61185789, -0.68776616,
       -0.7568025 , -0.81827711, -0.87157577, -0.91616594, -0.95160207,
       -0.97753012, -0.993691  , -0.99992326, -0.99616461, -0.98245261,
       -0.95892427, -0.92581468, -0.88345466, -0.83226744, -0.77276449,
       -0.70554033, -0.63126664, -0.55068554, -0.46460218, -0.37387666,
       -0.2794155 , -0.1821625 , -0.0830894 ])
In [5]:
# These data can then easily be plotted.
plt.plot(xx, f(xx), 'bo')
plt.xlabel('x')
plt.ylabel('f(x) = sin x');
In [6]:
# If you wanted to do symbolic math using a function, you could use 'sym.sin()'
# instead of 'np.sin()'.  This requires the module 'sympy'.
import sympy as sym
def f1(x):
    return sym.sin(x)
In [7]:
# Now we can take the x-derivative.  First, we now do need to specify 'x' as
# a symbol.
x = sym.Symbol('x')  
z = sym.diff(f1(x), x)
z
Out[7]:
$\displaystyle \cos{\left(x \right)}$
In [8]:
# Of course, we can integrate too. 
z = sym.integrate(f1(x), x)
z
Out[8]:
$\displaystyle - \cos{\left(x \right)}$
In [9]:
# Here is another function:
def g(x):
    return x**2
In [10]:
# We can nest the two functions:
f(g(1.41))
# which is equivalent to sin(1.41^2). 
Out[10]:
0.91418507525795
In [11]:
# We should be able to get Python to do chain rule for us:
z = sym.diff(g(f1(x)), x)
z
Out[11]:
$\displaystyle 2 \sin{\left(x \right)} \cos{\left(x \right)}$
In [12]:
# The integral:
z = sym.integrate(g(f1(x)), x)
z
Out[12]:
$\displaystyle \frac{x}{2} - \frac{\sin{\left(x \right)} \cos{\left(x \right)}}{2}$
In [13]:
# We should be able to go back to sin^2x:
sym.simplify(sym.diff(z, x))
Out[13]:
$\displaystyle \sin^{2}{\left(x \right)}$
In [14]:
# Everything above was for a function of a single variable.  You can, of
# course, have a function of any number of variables.  Here's a simple
# function of two variables:
def k(x, y):
    return x**2 + y**2
k(1, 2)
Out[14]:
5
In [15]:
# Some more symbolic derivatives
z = sym.diff(k(x, f1(x)), x)
z
Out[15]:
$\displaystyle 2 x + 2 \sin{\left(x \right)} \cos{\left(x \right)}$